package org.weso.acota.services;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.weso.acota.services.to.ListSuggestionsTO;
import org.weso.acota.to.RequestSuggestionTO;
import org.weso.acota.to.ResourceTO;

/**
 *
 * @author chema, weena
 */

@Path("/acota")
public class AcotaServiceRest {

    private AcotaServiceFacade facade;
    public AcotaServiceRest(){
        this.facade = new AcotaServiceFacadeImpl();
    }
    @GET
    @Path("oldSuggest")
    @Produces({"text/plain", "application/xml", "application/json"})
    public ListSuggestionsTO oldSuggest(@QueryParam("q") String q, @QueryParam("provider") String provider){
    	try{
    		
            RequestSuggestionTO request = new RequestSuggestionTO();
            return this.facade.suggest(request);
    	}catch(Exception e){
    		 throw new WebApplicationException(Response.Status.BAD_REQUEST);
    	}
    }
    
    @GET
    @Path("suggest")
    @Produces({"text/plain", "application/xml", "application/json"})
    public ListSuggestionsTO suggest(@QueryParam("title") String title, @QueryParam("description") String description, @QueryParam("uri") String uri){
    	try{
    		//System.out.println("Suggest: "+title+" description: "+description+" uri: "+uri); //FIXME: change to logger
    		RequestSuggestionTO request = new RequestSuggestionTO();
    		ResourceTO resource = new ResourceTO();
    			resource.setDescription(description);
    			resource .setLabel(title);
    			resource.setUri(uri);
    			request.setResource(resource);
            return this.facade.suggest(request);
    	}catch(Exception e){
    		 throw new WebApplicationException(Response.Status.BAD_REQUEST);
    	}
    }
    
    @GET
    @Path("suggestPaged")
    @Produces({"text/plain", "application/xml", "application/json"})
    public ListSuggestionsTO suggestPaged(
    		@QueryParam("title") String title, 
    		@QueryParam("description") String description, 
    		@QueryParam("uri") String uri,
    		@QueryParam("page")String page,
    		@QueryParam("max") String max){
    	try{
    		//System.out.println("Suggest: "+title+" description: "+description+" uri: "+uri); //FIXME: change to logger
    		int pageInt = 0;
    		int maxInt =  10;
    		if (!page.equals("") && Integer.valueOf(page)>=0){
    			pageInt = Integer.valueOf(page);
    		}
    		
    		if (!max.equals("") && Integer.valueOf(max)>0){
    			maxInt = Integer.valueOf(max);
    		}
    		
    		RequestSuggestionTO request = new RequestSuggestionTO();
    		ResourceTO resource = new ResourceTO();
    			resource.setDescription(description);
    			resource.setLabel(title);
    			resource.setUri(uri);
    			request.setResource(resource);
            return this.facade.suggestPaged(request,pageInt,maxInt);
    	}catch(Exception e){
    		 throw new WebApplicationException(Response.Status.BAD_REQUEST);
    	}
    }
}
